// by Jens Schmidt 2009
// for: YOU
// use, modify, copy this as you like

function ImageDetailsAssistant(param) {
	this.filename=param.filename;
	this.uploadQueue = new UploadQueue(this.dbCallback.bind(this));
	this.queueIndex = -1;
//	not yet: this.albums=new Albums();
}

ImageDetailsAssistant.prototype.dbCallback = function(state){
	if (state && state!='fail') {
		this.queueIndex = this.uploadQueue.findElement(this.filename);
		if (this.queueIndex >= 0) {
			var caption = '';
			var description = '';
			
			this.selectedPrivacy = this.uploadQueue.list[this.queueIndex].privacy;
			caption = this.uploadQueue.list[this.queueIndex].caption;
			description = this.uploadQueue.list[this.queueIndex].description;
			if (description) {
				this.controller.get("description").mojo.setValue(description);
			}
			if (caption) {
				this.controller.get("caption").mojo.setValue(caption);
			}
			
			this.cmdMenuModel.items[1].items[0] = {};
			this.controller.modelChanged(this.cmdMenuModel, this);
			
			this.privacymodel.value = this.selectedPrivacy;
			this.controller.modelChanged(this.privacymodel, this);
		}
	}
}

ImageDetailsAssistant.prototype.setup = function() {
	this.imageViewerWidget = this.controller.get('imageViewer');
	this.sendFn=this.send.bind(this);
	this.queueFn=this.queue.bind(this);
	this.viewFn=this.view.bind(this);
	
	this.viewerAttributes = {
		noExtractFS: true,
		highResolutionLoadTimeout: 3,
		limitZoom: false
	};
	
	this.viewerModel = {};
	this.controller.setupWidget('imageViewer', this.viewerAttributes, this.viewerModel);
	
	this.controller.setupWidget(Mojo.Menu.appMenu,
       	this.attributes = {
                omitDefaultItems: true
            },
    	this.model = {
        visible: true,
        items: []
		});
	
	var caption='';
	var description='';
	if (this.uploadQueue.dbState != 'started' &&
		this.uploadQueue.dbState != 'fail') {
		this.queueIndex = this.uploadQueue.findElement(this.filename);
		if (this.queueIndex >= 0) {
			this.selectedPrivacy = this.uploadQueue.list[this.queueIndex].privacy;
			caption = this.uploadQueue.list[this.queueIndex].caption;
			description = this.uploadQueue.list[this.queueIndex].description;
		}
		if (!description) {
			description = '';
		}
		if (caption) {
			caption = '';
		}
	}

	this.controller.setupWidget("caption",
         this.attributes = {
             hintText: $L('enter optional caption here'),
             multiline: false,
             enterSubmits: false,
             focus: true,
			 focusMode: Mojo.Widget.focusSelectMode,
//			 maxLength: 30,
			 width: 125
         },
         this.model = {
             value: caption,
             disabled: false
    });
	
	this.controller.setupWidget("description",
         this.attributes = {
             hintText: $L('enter optional description here'),
             multiline: true,
             enterSubmits: false,
             focus: false,
			 focusMode: Mojo.Widget.focusSelectMode,
			 width: 125
         },
         this.model = {
             value: description,
             disabled: false
    });
	this.selectedPrivacy=0;
	this.privacymodel={
        value: this.selectedPrivacy,
        disabled: false
        }
	this.controller.setupWidget("privacy",
        this.attributes = {
			choices: [{
				label: "Public",
				value: '0'
			}, {
				label: "Friends",
				value: '500'
			}, {
				label: "Family",
				value: '800'
			}, {
				label: "Private",
				value: '1000'
			}],
			label: 'Privacy'
		},
        this.privacymodel
    );
	Mojo.Event.listen(this.controller.get("privacy"), Mojo.Event.propertyChange, this.handlePrivacyUpdate.bind(this));

	this.cmdMenuModel = {
		visible: true,
		items: [{
			items: [{
				label: $L('Full View'),
				command: 'view'
			}]
		}, {
			items: [
			{
				label: $L('Queue'),
				command: 'queue'
			}, {
				label: $L('Upload'),
				command: 'send'
			}]
		}]
	};
	this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, this.cmdMenuModel);
}

ImageDetailsAssistant.prototype.handleCommand = function(event) {
		if  (event.type== Mojo.Event.command){
			switch (event.command) {
					case 'view':
						this.viewFn();
						break;
						
					case 'send':
						this.sendFn();
						break;
						
					case 'queue':
						this.queueFn();
						break;
				}
		}
}

ImageDetailsAssistant.prototype.activate = function(param) {
	this.imageViewerWidget.mojo.centerUrlProvided(this.filename);
	this.imageViewerWidget.mojo.manualSize(320, 120);
}

ImageDetailsAssistant.prototype.send = function(event) {
	if (this.queueIndex>=0){
		// sending this one up, can delete it from the queue
		this.uploadQueue.deleteElement(this.queueIndex);
	}
 	var selection={
		caption:this.controller.get("caption").mojo.getValue(),
		description:this.controller.get("description").mojo.getValue(),
		privacy:this.selectedPrivacy,
		action:'send'}
	this.controller.stageController.popScene(selection);
}

ImageDetailsAssistant.prototype.queue = function(event){
	var shownName=this.controller.get("caption").mojo.getValue();
	if (!shownName){
		var kram = this.filename.split("/");
		if (kram.length>1){
			shownName=kram[kram.length-1];	
		}else{
			kram.length=this.filename;
		}
	}
	var element = {
		filename: this.filename,
		caption: this.controller.get("caption").mojo.getValue(),
		description: this.controller.get("description").mojo.getValue(),
		privacy: this.selectedPrivacy,
		shownName: shownName
	};
	var err=this.uploadQueue.storeElement(this.queueIndex,element);
	this.controller.get("caption").update(Object.toJSON(err));
	this.controller.stageController.popScene();
}

ImageDetailsAssistant.prototype.view = function(event) {
	this.controller.stageController.pushScene("view-image",this.filename);
}
ImageDetailsAssistant.prototype.handlePrivacyUpdate = function(param){
	if (param.model) {
		this.selectedPrivacy = param.model.value;
	}
}
ImageDetailsAssistant.prototype.deactivate = function(event) {
}

ImageDetailsAssistant.prototype.cleanup = function(event) {
	if (this.queueIndex>=0){
		var shownName=this.controller.get("caption").mojo.getValue();
		if (!shownName){
			var kram = this.filename.split("/");
			if (kram.length>1){
				shownName=kram[kram.length-1];	
			}else{
				kram.length=this.filename;
			}
		}
		var element={
		filename:this.filename,
		caption:this.controller.get("caption").mojo.getValue(),
		description:this.controller.get("description").mojo.getValue(),
		privacy:this.selectedPrivacy,
		shownName:shownName}
		this.uploadQueue.storeElement(this.queueIndex,element);
	}
};